BPlusTree/BPlusTree.Test/TestBackupAndRecovery.cs
C# | Copy Code |
---|---|
using (TempFile savelog = new TempFile()) using (TempFile temp = new TempFile()) { var options = GetOptions(temp); options.FileBlockSize = 512; options.StoragePerformance = StoragePerformance.Fastest; options.CalcBTreeOrder(Marshal.SizeOf(typeof(Guid)), Marshal.SizeOf(typeof(TestInfo))); options.TransactionLog = new TransactionLog<Guid, TestInfo>( new TransactionLogOptions<Guid, TestInfo>( options.TransactionLogFileName, options.KeySerializer, options.ValueSerializer ) ); //Now recover... Dictionary<Guid, TestInfo> first = new Dictionary<Guid, TestInfo>(); Dictionary<Guid, TestInfo> sample; using (var tree = new BPlusTree<Guid, TestInfo>(options)) { tree.EnableCount(); Insert(tree, first, 1, 100, TimeSpan.FromMinutes(1)); tree.Commit(); Assert.AreEqual(100, tree.Count); sample = new Dictionary<Guid, TestInfo>(first); Insert(tree, sample, 7, 5000, TimeSpan.FromMinutes(1)); Assert.AreEqual(35100, tree.Count); for (int i = 0; i < 1; i++) { foreach (var rec in tree) { var value = rec.Value; value.UpdateCount++; value.ReadCount++; tree[rec.Key] = value; } } File.Copy(options.TransactionLog.FileName, savelog.TempPath, true); tree.Rollback(); TestInfo.AssertEquals(first, tree); } //file still has initial committed data TestInfo.AssertEquals(first, BPlusTree<Guid, TestInfo>.EnumerateFile(options)); //restore the log and verify all data. File.Copy(savelog.TempPath, options.TransactionLog.FileName, true); using (var tree = new BPlusTree<Guid, TestInfo>(options)) { TestInfo.AssertEquals(sample, tree); } //file still has initial committed data TestInfo.AssertEquals(sample, BPlusTree<Guid, TestInfo>.EnumerateFile(options)); } |
VB.NET | Copy Code |
---|---|
Using savelog As New TempFile() Using temp As New TempFile() Dim options As var = GetOptions(temp) options.FileBlockSize = 512 options.StoragePerformance = StoragePerformance.Fastest options.CalcBTreeOrder(Marshal.SizeOf(GetType(Guid)), Marshal.SizeOf(GetType(TestInfo))) options.TransactionLog = New TransactionLog(Of Guid, TestInfo)(New TransactionLogOptions(Of Guid, TestInfo)(options.TransactionLogFileName, options.KeySerializer, options.ValueSerializer)) 'Now recover... Dim first As New Dictionary(Of Guid, TestInfo)() Dim sample As Dictionary(Of Guid, TestInfo) Using tree As var = New BPlusTree(Of Guid, TestInfo)(options) tree.EnableCount() Insert(tree, first, 1, 100, TimeSpan.FromMinutes(1)) tree.Commit() Assert.AreEqual(100, tree.Count) sample = New Dictionary(Of Guid, TestInfo)(first) Insert(tree, sample, 7, 5000, TimeSpan.FromMinutes(1)) Assert.AreEqual(35100, tree.Count) Dim i As Integer = 0 While i < 1 For Each rec As var In tree Dim value As var = rec.Value System.Math.Max(System.Threading.Interlocked.Increment(value.UpdateCount),value.UpdateCount - 1) System.Math.Max(System.Threading.Interlocked.Increment(value.ReadCount),value.ReadCount - 1) tree(rec.Key) = value Next System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1) End While File.Copy(options.TransactionLog.FileName, savelog.TempPath, True) tree.Rollback() TestInfo.AssertEquals(first, tree) End Using 'file still has initial committed data TestInfo.AssertEquals(first, BPlusTree(Of Guid, TestInfo).EnumerateFile(options)) 'restore the log and verify all data. File.Copy(savelog.TempPath, options.TransactionLog.FileName, True) Using tree As var = New BPlusTree(Of Guid, TestInfo)(options) TestInfo.AssertEquals(sample, tree) End Using 'file still has initial committed data TestInfo.AssertEquals(sample, BPlusTree(Of Guid, TestInfo).EnumerateFile(options)) End Using End Using |
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7